<template>
  <el-row>
    <el-card>
      <el-form ref="searchForm" :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item :label="$t('channelData.date')">
          <el-date-picker
            v-model="time"
            type="daterange"
            range-separator="~"
            :start-placeholder="$t('common.startDate')"
            :end-placeholder="$t('common.endDate')"
          />
        </el-form-item>
        <el-form-item :label="$t('channelData.table.packageName')">
          <el-select v-model="searchForm.packageName" :placeholder="$t('channelData.prompt.packageName')" style="width: 100%">
            <el-option
              v-for="item in allPackageData"
              :key="item.packageName"
              :label="item.packageName"
              :value="item.packageName"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('channelData.table.channelName')">
          <el-input v-model="searchForm.channelName" :placeholder="$t('channelData.prompt.channelName')" :maxlength="50" />
        </el-form-item>
        <el-row>
          <el-button type="primary" @click="search">
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="reset">
            {{ $t('common.reset') }}
          </el-button>
          <el-button :disabled="!(totalNumber > 0)" @click="exportList">
            {{ $t('common.ex') }}
          </el-button>
        </el-row>
      </el-form>
    </el-card>
    <el-card>
      <el-table
        v-loading="loading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="tableData"
        border
        :stripe="true"
        style="width: 100%;"
      >
        <el-table-column
          fixed="left"
          prop="channelName"
          :label="tableText['channelName']"
          min-width="120"
        />
        <el-table-column
          prop="packageName"
          :label="tableText['packageName']"
          min-width="150"
        />
        <el-table-column
          prop="googleLink"
          :label="tableText['channelLink']"
          min-width="300"
        />
        <el-table-column
          prop="downloadCount"
          :label="tableText['downLoad']"
          min-width="120"
        >
          <template slot-scope="scope">
            {{ scope.row['downloadCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="registerCount"
          :label="tableText['registered']"
          min-width="170"
        >
          <template slot-scope="scope">
            {{ scope.row['registerCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="firstLoanApplyUserCount"
          :label="tableText['firstLoanApplication']"
          min-width="170"
        >
          <template slot-scope="scope">
            {{ scope.row['firstLoanApplyUserCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="firstLoanPassUserCount"
          :label="tableText['firstLoanReviewPassed']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['firstLoanPassUserCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="firstLoanPaymentUserCount"
          :label="tableText['firstLoanSuccessNumber']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['firstLoanPaymentUserCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="firstLoanFinishUserCount"
          :label="tableText['completeTheFirstLoan']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['firstLoanFinishUserCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="secondLoanFinishUserCount"
          :label="tableText['completeTheSecondLoan']"
          min-width="170"
        >
          <template slot-scope="scope">
            {{ scope.row['secondLoanFinishUserCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="multiLoanFinishUserCount"
          :label="tableText['completeMultiLoan']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['multiLoanFinishUserCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="repaymentSuccessOrderCount"
          :label="tableText['successfulRepayment']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['repaymentSuccessOrderCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="firstLoanOverdueUserCount"
          :label="tableText['firstLoanOverdue']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['firstLoanOverdueUserCount'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="m1OverdueOrderCount"
          :label="tableText['M1IsOverdue']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['m1Count'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="m2OverdueOrderCount"
          :label="tableText['M2IsOverdue']"
          min-width="150"
        >
          <template slot-scope="scope">
            {{ scope.row['m2Count'] || 0 }}
          </template>
        </el-table-column>
        <el-table-column
          prop="m3OverdueOrderCount"
          :label="tableText['M3IsOverdue']"
          min-width="170"
        >
          <template slot-scope="scope">
            {{ scope.row['m3Count'] || 0 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          :current-page="searchForm.currentPage"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="searchForm.pageSize"
          :total="totalNumber"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </el-row>
</template>

<script>
import { gaTotal, gaTotalExport } from '@/api/operation'
import { allPackage } from '@/api/user'
import { formatDate } from '@/utils/method'
export default {
  name: 'ChannelGaData',
  data() {
    return {
      loading: false,
      time: null,
      tableData: [],
      allPackageData: [],
      tableText: {},
      totalNumber: 0,
      searchForm: this.params()
    }
  },
  mounted() {
    this.tableText = this.$t('channelData.table')
    this.getData()
    this.getAllPackage()
  },
  methods: {
    params() {
      return {
        currentPage: 1,
        pageSize: 10,
        channelName: null,
        start: null,
        end: null,
        packageName: null,
        columns: ['state'],
        orderBy: [true]
      }
    },
    search() {
      this.searchForm.currentPage = 1
      this.getData()
    },
    reset() {
      this.searchForm = this.params()
      this.time = null
      this.getData()
    },
    getAllPackage() {
      allPackage()
        .then(res => {
          if (res.success) {
            this.allPackageData = res.data.filter(v => {
              return v.packageName
            })
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },
    getData() {
      this.loading = true
      const data = this.searchData()
      gaTotal(data)
        .then(res => {
          this.loading = false
          if (res.success) {
            this.tableData = res.data
            this.totalNumber = res.page ? res.page['totalNumber'] : 0
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error(err.message)
        })
    },
    handleSizeChange(val) {
      this.searchForm.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.searchForm.currentPage = val
      this.getData()
    },
    exportList() {
      gaTotalExport(this.searchData())
    },
    searchData() {
      const data = this.searchForm
      if (this.time && this.time.length > 1) {
        data['start'] = formatDate(new Date(this.time[0]), 'yyyy-MM-dd') + ' 00:00:00'
        data['end'] = formatDate(new Date(this.time[1]), 'yyyy-MM-dd') + ' 23:59:59'
      } else {
        data['start'] = null
        data['end'] = null
      }
      return data
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-card{
    margin: 10px;
  }
</style>

